import 'dart:developer';

import 'package:intl/intl.dart';
import 'package:sports_new/entity/match/expert_views_entity.dart';

class PlanInfoEntity {
  int? activityId;
  String? activityName;
  String? beginTime;
  String? saleEndTime;
  String? expertId;
  String? expertLogo;
  String? expertName;
  String? firstTag;
  int? isInvite;
  int? sportsId;
  List<PlanItemShowEntity>? itemShow;
  List<MatchBries>? matchBries;
  String? payCnt;
  String? planContent;
  int? planId;
  String? planIsOrder;
  String? planIssueNo;
  String? planPublicTime;
  int? planStatus;
  String? planSummary;
  String? planTitle;
  String? price;
  String? priceReal;
  List<PlanEntity>? pushPlan;
  String? secondTag;
  int? isFocus;
  int? pv;
  int? isCanPay;
  int? isCanRead;
  String? footerContent;
  int? isShowFooter;
  String? feeContent;
  int? feeContentCnt;
  String? freeContent;
  int? vipBtn;
  String? vipContent;
  int? isVideo;
  String? videoUrl;

  PlanInfoEntity(
      {this.activityId,
      this.activityName,
      this.beginTime,
      this.saleEndTime,
      this.expertId,
      this.expertLogo,
      this.expertName,
      this.firstTag,
      this.isInvite,
      this.sportsId,
      this.itemShow,
      this.matchBries,
      this.payCnt,
      this.planContent,
      this.planId,
      this.planIsOrder,
      this.planIssueNo,
      this.planPublicTime,
      this.planStatus,
      this.planSummary,
      this.planTitle,
      this.price,
      this.priceReal,
      this.pushPlan,
      this.secondTag,
      this.feeContent,
      this.feeContentCnt,
      this.freeContent,
      this.vipBtn,
      this.vipContent});

  PlanInfoEntity.fromJson(Map<String, dynamic> json) {
    // log('plan info entity $json');
    activityId = json['activityId'];
    activityName = json['activityName'];
    beginTime = json['beginTime'];
    saleEndTime = json['saleEndTime'];
    expertId = json['expertId'];
    expertLogo = json['expertLogo'];
    expertName = json['expertName'];
    firstTag = json['firstTag'];
    isInvite = json['isInvite'];
    sportsId = json['sportsId'];
    if (json['itemShow'] != null) {
      itemShow = <PlanItemShowEntity>[];
      json['itemShow'].forEach((v) {
        if (v == null) {
          return;
        }
        itemShow!.add(new PlanItemShowEntity.fromJson(v));
      });
    }
    if (json['matchBries'] != null) {
      matchBries = <MatchBries>[];
      json['matchBries'].forEach((v) {
        if (v == null) {
          return;
        }
        matchBries!.add(new MatchBries.fromJson(v));
      });
    }
    payCnt = json['payCnt'];
    planContent = json['planContent'];
    planId = json['planId'];
    planIsOrder = json['planIsOrder'];
    planIssueNo = json['planIssueNo'];
    planPublicTime = json['planPublicTime'];
    planStatus = json['planStatus'];
    planSummary = json['planSummary'];
    planTitle = json['planTitle'];
    price = json['price'];
    priceReal = json['priceReal'];
    // if (json['pushPlan'] != null) {
    //   pushPlan = [];
    //   (json['pushPlan'] as List).forEach((element) { pushPlan!.add(Rows.fromJson(element)); });
    // }
    pushPlan = json['pushPlan'] != null
        ? (json['pushPlan'] as List).map((e) => PlanEntity.fromJson(e)).toList()
        : null;
    secondTag = json['secondTag'];
    pv = json['pv'];
    isFocus = json['isFocus'];
    isCanPay = json['isCanPay'];
    isCanRead = json['isCanRead'];
    footerContent = json['footerContent'];
    isShowFooter = json['isShowFooter'];
    feeContent = json['feeContent'];
    feeContentCnt = json['feeContentCnt'];
    freeContent = json['freeContent'];
    vipBtn = json['vipBtn'];
    vipContent = json['vipContent'];
    isVideo = json['isVideo'];
    videoUrl = json['videoUrl'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['activityId'] = this.activityId;
    data['activityName'] = this.activityName;
    data['beginTime'] = this.beginTime;
    data['saleEndTime'] = this.saleEndTime;
    data['expertId'] = this.expertId;
    data['expertLogo'] = this.expertLogo;
    data['expertName'] = this.expertName;
    data['firstTag'] = this.firstTag;
    data['isInvite'] = this.isInvite;
    data['sportsId'] = this.sportsId;
    if (this.itemShow != null) {
      data['itemShow'] = this.itemShow!.map((v) => v.toJson()).toList();
    }
    if (this.matchBries != null) {
      data['matchBries'] = this.matchBries!.map((v) => v.toJson()).toList();
    }
    data['payCnt'] = this.payCnt;
    data['planContent'] = this.planContent;
    data['planId'] = this.planId;
    data['planIsOrder'] = this.planIsOrder;
    data['planIssueNo'] = this.planIssueNo;
    data['planPublicTime'] = this.planPublicTime;
    data['planStatus'] = this.planStatus;
    data['planSummary'] = this.planSummary;
    data['planTitle'] = this.planTitle;
    data['price'] = this.price;
    data['priceReal'] = this.priceReal;
    if (this.pushPlan != null) {
      data['pushPlan'] = this.pushPlan!.map((e) => e.toJson()).toList();
    }
    data['secondTag'] = this.secondTag;
    data['pv'] = pv;
    data['isFocus'] = isFocus;
    data['isCanPay'] = isCanPay;
    data['isCanRead'] = isCanRead;
    data['footerContent'] = footerContent;
    data['isShowFooter'] = isShowFooter;
    data['feeContent'] = feeContent;
    data['feeContentCnt'] = feeContentCnt;
    data['freeContent'] = freeContent;
    data['vipBtn'] = this.vipBtn;
    data['vipContent'] = this.vipContent;
    data['isVideo'] = isVideo;
    data['videoUrl'] = videoUrl;
    return data;
  }
}

class PlanPlayContentItem {
  String? i;
  String? n;
  String? o;
  String? p;
  int? r;
  int? s;

  PlanPlayContentItem({this.i, this.n, this.o, this.p, this.r, this.s});

  PlanPlayContentItem.fromJson(Map<String, dynamic> json) {
    i = json['i'];
    n = json['n'];
    o = json['o'];
    p = json['p'];
    r = json['r'];
    s = json['s'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['i'] = this.i;
    data['n'] = this.n;
    data['o'] = this.o;
    data['p'] = this.p;
    data['r'] = this.r;
    data['s'] = this.s;
    return data;
  }
}

class PlanPlayContent {
  String? line;
  List<PlanPlayContentItem>? list;
  int? status;
  int? used;

  PlanPlayContent({this.line, this.list, this.status, this.used});

  PlanPlayContent.fromJson(Map<String, dynamic> json) {
    line = json['line'];
    if (json['list'] != null) {
      list = <PlanPlayContentItem>[];
      json['list'].forEach((v) {
        list!.add(new PlanPlayContentItem.fromJson(v));
      });
    }
    status = json['status'];
    used = json['used'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['line'] = this.line;
    if (this.list != null) {
      data['list'] = this.list!.map((v) => v.toJson()).toList();
    }
    data['status'] = this.status;
    data['used'] = this.used;
    return data;
  }
}

class PlanItemShowEntity {
  PlanPlayContent? bf;
  PlanPlayContent? bqc;
  List<PlanPlayContent>? dx;
  List<PlanPlayContent>? sp;
  PlanPlayContent? jqs;
  int? matchId;
  int? playType;
  String? playTypeName;
  String? appPlayTypeName;
  List<PlanPlayContent>? rqSpf;
  PlanPlayContent? spf;
  int? status;
  List<PlanPlayContent>? yp;
  List<PlanPlayContent>? lqDx;
  List<PlanPlayContent>? lqRqSf;
  PlanPlayContent? lqSf;

  PlanItemShowEntity(
      {this.bf,
      this.bqc,
      this.dx,
      this.jqs,
      this.matchId,
      this.playType,
      this.playTypeName,
      this.appPlayTypeName,
      this.rqSpf,
      this.spf,
      this.status,
      this.yp,
      this.lqDx,
      this.lqRqSf,
      this.lqSf,
      this.sp});

  List<PlanPlayContent>? getContentFrom(dynamic l) {
    log("$l");
    if (l == null || l is! List) {
      return null;
    }
    return l.map((e) => PlanPlayContent.fromJson(e)).toList();
  }

  PlanItemShowEntity.fromJson(Map<String, dynamic> json) {
    // bf = json['bf'] != null ? new PlanPlayContentEntity.fromJson(json['bf']) : null;
    bf = json['bf'] == null ? null : PlanPlayContent.fromJson(json['bf']);
    bqc = json['bqc'] == null ? null : PlanPlayContent.fromJson(json['bqc']);
    dx = getContentFrom(json['dx']);
    jqs = json['jqs'] == null ? null : PlanPlayContent.fromJson(json['jqs']);
    matchId = json['matchId'];
    playType = json['playType'];
    playTypeName = json['playTypeName'];
    appPlayTypeName = json['appPlayTypeName'];
    rqSpf = getContentFrom(json['rqSpf']);
    spf = json['spf'] == null ? null : PlanPlayContent.fromJson(json['spf']);

    status = json['status'];

    yp = getContentFrom(json['yp']);
    sp = getContentFrom(json['sp']);
    // yp = json['yp'] != null ? PlanPlayContentEntity.fromJson(json['yp']) : null;
    lqDx = getContentFrom(json['lqDx']);
    lqRqSf = getContentFrom(json['lqRqSf']);
    lqSf = json['lqSf'] == null ? null : PlanPlayContent.fromJson(json['lqSf']);
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.bf != null) {
      data['bf'] = this.bf!.toJson();
    }
    if (this.bqc != null) {
      data['bqc'] = this.bqc!.toJson();
    }
    if (this.dx != null) {
      data['dx'] = this.dx!.map((e) => e.toJson());
    }
    if (this.jqs != null) {
      data['jqs'] = this.jqs!.toJson();
    }
    data['matchId'] = this.matchId;
    data['playType'] = this.playType;
    data['playTypeName'] = this.playTypeName;
    data['appPlayTypeName'] = this.appPlayTypeName;
    if (this.rqSpf != null) {
      data['rqSpf'] = this.rqSpf!.map((e) => e.toJson());
    }
    if (this.spf != null) {
      data['spf'] = this.spf!.toJson();
    }
    data['status'] = this.status;
    if (this.yp != null) {
      data['yp'] = this.yp!.map(((e) => e.toJson()));
    }
    if (this.sp != null) {
      data['sp'] = this.sp!.map((e) => e.toJson());
    }
    data['lqDx'] = lqDx?.map((e) => e.toJson());
    data['lqRqSf'] = lqRqSf?.map((e) => e.toJson());
    data['lqSf'] = lqSf?.toJson();
    return data;
  }
}

// PlanItemShowEntity status
// TYPE_0(0,"未开"),
// TYPE_1(1,"中"),
// TYPE_2(2,"未中"),
// TYPE_3(3,"取消");

final _df = DateFormat("yyyy-MM-dd HH:mm:ss");

extension PlanMatchBriesEntityEx1 on MatchBries {
  DateTime? get matchTimeDate => DateTime.tryParse(matchTime ?? "")?.toLocal();
  bool get isMatchStart =>
      ((matchTimeDate?.difference(DateTime.now()))?.inSeconds ?? 1) < 0;
}

extension PlanInfoEx1 on PlanInfoEntity {
  DateTime? get pubTime => DateTime.tryParse(planPublicTime ?? "")?.toLocal();
  MatchBries? get match =>
      (matchBries?.isEmpty ?? true) ? null : matchBries?.first;
  DateTime? get matchTime => DateTime.tryParse(beginTime ?? "")?.toLocal();
  // bool? get isMatchStart => (matchTime?.difference(DateTime.now()).inSeconds ?? 1) < 0;

  int get uvCount => int.tryParse(payCnt ?? "") ?? 0;

  bool get hasPayed => (int.tryParse(planIsOrder ?? "") ?? 0) > 0;
  // bool get getIsFree => (isFree ?? 0) > 0;
  bool get canRead => (isCanRead ?? 0) > 0;
  bool get canPay => (isCanPay ?? 0) > 0;
  bool get showFooter => (isShowFooter ?? 0) > 0;
}
